package org.htmlparser.scanners;

import java.util.Vector;
import org.htmlparser.Attribute;
import org.htmlparser.Node;
import org.htmlparser.NodeFactory;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Cursor;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class CompositeTagScanner extends TagScanner
{
  private static final boolean mLeaveEnds;
  private static final boolean mUseJVMStack;

  protected void addChild(Tag paramTag, Node paramNode)
  {
    if (paramTag.getChildren() == null)
      paramTag.setChildren(new NodeList());
    paramNode.setParent(paramTag);
    paramTag.getChildren().add(paramNode);
  }

  protected Tag createVirtualEndTag(Tag paramTag, Lexer paramLexer, Page paramPage, int paramInt)
    throws ParserException
  {
    String str = "/" + paramTag.getRawTagName();
    Vector localVector = new Vector();
    localVector.addElement(new Attribute(str, (String)null));
    return paramLexer.getNodeFactory().createTagNode(paramPage, paramInt, paramInt, localVector);
  }

  protected void finishTag(Tag paramTag, Lexer paramLexer)
    throws ParserException
  {
    if (paramTag.getEndTag() == null)
      paramTag.setEndTag(createVirtualEndTag(paramTag, paramLexer, paramLexer.getPage(), paramLexer.getCursor().getPosition()));
    paramTag.getEndTag().setParent(paramTag);
    paramTag.doSemanticAction();
  }

  public final boolean isTagToBeEndedFor(Tag paramTag1, Tag paramTag2)
  {
    String[] arrayOfString;
    int j;
    int i = 0;
    String str = paramTag2.getTagName();
    if (paramTag2.isEndTag())
    {
      arrayOfString = paramTag1.getEndTagEnders();
      j = 0;
    }
    while (true)
    {
      while (true)
      {
        if (j < arrayOfString.length)
        {
          if (!(str.equalsIgnoreCase(arrayOfString[j])))
            break;
          i = 1;
        }
        return i;
        arrayOfString = paramTag1.getEnders();
      }
      ++j;
    }
  }

  public Tag scan(Tag paramTag, Lexer paramLexer, NodeList paramNodeList)
    throws ParserException
  {
    Tag localTag2;
    String str;
    Object localObject1 = paramTag;
    if (((Tag)localObject1).isEmptyXmlTag())
    {
      ((Tag)localObject1).setEndTag((Tag)localObject1);
      finishTag((Tag)localObject1, paramLexer);
      return localObject1;
    }
    Object localObject2 = paramLexer.nextNode(false);
    if (localObject2 != null)
    {
      if (!(localObject2 instanceof Tag))
        break label617;
      localTag2 = (Tag)localObject2;
      str = localTag2.getTagName();
      if ((!(localTag2.isEndTag())) || (!(str.equals(((Tag)localObject1).getTagName()))))
        break label193;
      ((Tag)localObject1).setEndTag(localTag2);
      localObject2 = null;
    }
    if (localObject2 == null)
    {
      int i = paramNodeList.size();
      if (i != 0)
      {
        localObject2 = paramNodeList.elementAt(i - 1);
        if (!(localObject2 instanceof Tag))
          break label641;
        Tag localTag1 = (Tag)localObject2;
        if (localTag1.getThisScanner() != this)
          break label635;
        paramNodeList.remove(i - 1);
        finishTag((Tag)localObject1, paramLexer);
        addChild(localTag1, (Node)localObject1);
        localObject1 = localTag1;
      }
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    label193: Scanner localScanner2;
                    while (true)
                    {
                      while (true)
                      {
                        while (true)
                        {
                          while (true)
                            if (localObject2 != null);
                          if (!(isTagToBeEndedFor((Tag)localObject1, localTag2)))
                            break;
                          paramLexer.setPosition(localTag2.getStartPosition());
                          localObject2 = null;
                        }
                        if (localTag2.isEndTag())
                          break label336;
                        localScanner2 = localTag2.getThisScanner();
                        if (localScanner2 == null)
                          break label325;
                        if (localScanner2 != this)
                          break label301;
                        if (!(localTag2.isEmptyXmlTag()))
                          break;
                        localTag2.setEndTag(localTag2);
                        finishTag(localTag2, paramLexer);
                        addChild((Tag)localObject1, localTag2);
                      }
                      paramNodeList.add((Node)localObject1);
                      localObject1 = localTag2;
                    }
                    label301: localObject2 = localScanner2.scan(localTag2, paramLexer, paramNodeList);
                    addChild((Tag)localObject1, (Node)localObject2);
                  }
                  label325: addChild((Tag)localObject1, localTag2);
                }
                label336: Vector localVector = new Vector();
                Attribute localAttribute = new Attribute(str, null);
                localVector.addElement(localAttribute);
                Scanner localScanner1 = paramLexer.getNodeFactory().createTagNode(paramLexer.getPage(), localTag2.getStartPosition(), localTag2.getEndPosition(), localVector).getThisScanner();
                if ((localScanner1 == null) || (localScanner1 != this))
                  break label606;
                int j = -1;
                int k = paramNodeList.size() - 1;
                if ((-1 == j) && (k >= 0))
                {
                  Tag localTag4 = (Tag)paramNodeList.elementAt(k);
                  if (str.equals(localTag4.getTagName()))
                    j = k;
                  while (true)
                  {
                    do
                      while (true)
                        --k;
                    while (!(isTagToBeEndedFor(localTag4, localTag2)));
                    j = k;
                  }
                }
                if (-1 == j)
                  break;
                finishTag((Tag)localObject1, paramLexer);
                addChild((Tag)paramNodeList.elementAt(paramNodeList.size() - 1), (Node)localObject1);
                for (int l = paramNodeList.size() - 1; l > j; --l)
                {
                  Tag localTag3 = (Tag)paramNodeList.remove(l);
                  finishTag(localTag3, paramLexer);
                  addChild((Tag)paramNodeList.elementAt(l - 1), localTag3);
                }
                localObject1 = (Tag)paramNodeList.remove(j);
                localObject2 = null;
              }
              addChild((Tag)localObject1, localTag2);
            }
            label606: addChild((Tag)localObject1, localTag2);
          }
          label617: addChild((Tag)localObject1, (Node)localObject2);
          ((Node)localObject2).doSemanticAction();
        }
        label635: localObject2 = null;
      }
      label641: localObject2 = null;
    }
  }
}